# -*- coding: utf-8 -*-
from pandas import DataFrame

n = 10                               # 值可更改
Xi = [-1 + i * 2 / n for i in range(n + 2)]
F = [1 / (1 + 25 * (x**2)) for x in Xi]
X = [-1, -0.8, -0.6, -0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8, 1]
s = []


for x in X:
    L = 0
    for i in range(n + 1):
        if i == 0:
            def l(i):
                if (x >= Xi[0]) and (x <= Xi[1]):
                    return (x - Xi[1]) / ((Xi[0] - Xi[1]))
                else:
                    return 0
        elif i > 0 and i < n:
            def l(i):
                if x >= Xi[i - 1] and x <= Xi[i]:
                    return (x - Xi[i - 1]) / ((Xi[i] - Xi[i - 1]))
                elif x >= Xi[i] and x <= Xi[i + 1]:
                    return (x - Xi[i + 1]) / (Xi[1] - Xi[i + 1])
                else:
                    return 0
        else:
            def l(i):
                if x >= Xi[i - 1] and x <= Xi[i]:
                    return (x - Xi[i - 1]) / (Xi[i] - Xi[i - 1])
                else:
                    return 0

        L = L + l(i) * F[i]
    s.append(L)
S = DataFrame(s, columns=['L10(x)'], index=X)
S.index.name = 'x'
print(S)
